﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BackupLibrary;
using MasterLibrary.Windows.Forms;

namespace Prometheus
{
    public partial class BackupItemForm : FormBase
    {
        public BackupItem backupItem { get; set; }

        public BackupItemForm(BackupItem backupItem, bool editMode)
        {
            InitializeComponent();

            this.backupItem = backupItem;

            if (backupItem.Type == ItemType.File)
            {
                this.tabControl.TabPages.Remove(tabTypes);
                this.chkIncludeSubfolders.Visible = false;
                
                if (editMode)
                    this.Text = "Datei bearbeiten";
                else
                    this.Text = "Datei hinzufügen";
            }
            else
            {
                if (editMode)
                    this.Text = "Ordner bearbeiten";
                else
                    this.Text = "Ordner hinzufügen";
            }

            if (editMode == true)
            {
                this.txtSourcePath.Text = this.backupItem.SourcePath;
                this.chkIncludeSubfolders.Checked = this.backupItem.IncludeSubfolder;
                this.chkAddSourcePath.Checked = this.backupItem.AddSourceFolder;

                this.radioTypesAll.Checked = (this.backupItem.FileType == ItemFileType.BackupAll) ? true : false;
                this.radioTypesExclude.Checked = (this.backupItem.FileType == ItemFileType.ExcludeContext) ? true : false;
                this.txtTypesExclude.Text = this.backupItem.ExcludeContext;
                this.radioTypesInclude.Checked = (this.backupItem.FileType == ItemFileType.IncludeContext) ? true : false;
                this.txtTypesInclude.Text = this.backupItem.IncludeContext;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!(this.txtSourcePath.Text.Length > 0))
            {
                this.errorProvider.SetError(this.txtSourcePath, "Bitte einen gültigen Quellpfad angeben.");
                return;
            }
            
            if (this.backupItem == null)
            {
                this.backupItem = new BackupItem();
            }
            this.backupItem.AddSourceFolder = this.chkAddSourcePath.Checked;
            this.backupItem.SourcePath = this.txtSourcePath.Text;
            this.backupItem.IncludeSubfolder = this.chkIncludeSubfolders.Checked;
            if (this.radioTypesAll.Checked)
            {
                this.backupItem.FileType = ItemFileType.BackupAll;
            }
            else if (this.radioTypesExclude.Checked)
            {
                this.backupItem.FileType = ItemFileType.ExcludeContext;
            }
            else
            {
                this.backupItem.FileType = ItemFileType.IncludeContext;
            }
            this.backupItem.ExcludeContext = this.txtTypesExclude.Text;
            this.backupItem.IncludeContext = this.txtTypesInclude.Text;
            this.backupItem.Active = true;

            this.DialogResult = DialogResult.OK;
        }

        private void btnSearchSource_Click(object sender, EventArgs e)
        {
            if (this.backupItem.Type == ItemType.File)
            {
                this.openFileDialog.RestoreDirectory = true;
                DialogResult result = this.openFileDialog.ShowDialog(this);

                if (result == DialogResult.OK)
                {
                    this.txtSourcePath.Text = this.openFileDialog.FileName;
                }
            }
            else
            {
                if (this.txtSourcePath.Text.Length > 0)
                {
                    this.folderBrowserDialog.SelectedPath = this.txtSourcePath.Text;
                }
                else
                {
                    this.folderBrowserDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
                }
                this.folderBrowserDialog.ShowNewFolderButton = false;
                DialogResult result = this.folderBrowserDialog.ShowDialog(this);

                if (result == DialogResult.OK)
                {
                    this.txtSourcePath.Text = this.folderBrowserDialog.SelectedPath;
                }
            }
        }

        private void chkAddSourcePath_CheckedChanged(object sender, EventArgs e)
        {
            this.backupItem.AddSourceFolder = this.chkAddSourcePath.Checked;
        }
    }
}
